package com.ruoyi.common.utils.file;


import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * MultipartFile 转 File 工具类
 *
 * @ClassName MultipartFileToFile
 * @Description MultipartFile转fie
 * @Author TongGuoBo
 * @Date 2019/6/19 13:48
 **/
public class MultipartFileToFile {
    public static File MultipartFileToFile(MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        //文件夹
        Path path2 = Paths.get("/home/ruoyi/file");
        //具体文件
        String substring = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        String name = System.currentTimeMillis() +substring;
        Path path = Paths.get( "/home/ruoyi/file/"+ name);
        File file1 = path.toFile();
        //创建文件夹
        if (!file1.exists()) {
            path2.toFile().mkdirs();
        }
        Files.write(path, bytes);//创建文件
        return file1;
    }

}